package com.jaya.budan.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.CommonPopWindow;

import org.json.JSONException;
import org.json.JSONObject;

public class My_Wallet_Withdraw_Activity extends BaseActivity implements View.OnClickListener, CommonPopWindow.ViewClickListener {

    private RelativeLayout back;
    private Button btn_login;
    private TextView tv_wallet_withdraw_guize, tv_wallet_withdraw_limit_money, tv_wallet_withdraw_limit_money_yb;
    private String access_token, money, limit_money, fee_lv;
    private int is_certify;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet_withdraw);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        money = intent.getStringExtra("money");
        is_certify = intent.getIntExtra("is_certify", 0);
        back = findViewById(R.id.back);
        btn_login = findViewById(R.id.btn_login);
        tv_wallet_withdraw_guize = findViewById(R.id.tv_wallet_withdraw_guize);
        tv_wallet_withdraw_limit_money = findViewById(R.id.tv_wallet_withdraw_limit_money);
        tv_wallet_withdraw_limit_money_yb = findViewById(R.id.tv_wallet_withdraw_limit_money_yb);
        back.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        tv_wallet_withdraw_guize.setOnClickListener(this);
        postData();
        tv_wallet_withdraw_limit_money.setText("" + money);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                if (is_certify == 0){
                    CommonPopWindow.newBuilder()
                            .setView(R.layout.pop_geren_fans_cancle)
                            .setAnimationStyle(R.style.AnimUp)
                            .setBackgroundDrawable(new BitmapDrawable())
                            .setSize(0, 600)
                            .setViewOnClickListener(this)
                            .setBackgroundDarkEnable(true)
                            .setBackgroundAlpha(0.7f)
                            .build(this)
                            .showAsCenter(v);
                } else {
                    Intent intent = new Intent(this, My_Wallet_Withdraw2_Activity.class);
                    intent.putExtra("access_token", access_token);
                    intent.putExtra("money", tv_wallet_withdraw_limit_money_yb.getText().toString().trim());
                    intent.putExtra("limit_money", limit_money);
                    intent.putExtra("fee_lv", fee_lv);
                    startActivity(intent);
                }
                break;
            case R.id.tv_wallet_withdraw_guize:
                Intent intent1 = new Intent(this, Privacy_WebView_Activity.class);
                intent1.putExtra("access_token", access_token);
                intent1.putExtra("type", 10);//兑换提现原则
                startActivity(intent1);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void getChildView(final PopupWindow mPopupWindow, View view, int mLayoutResId) {
        switch (mLayoutResId) {
            case R.layout.pop_geren_fans_cancle:
                TextView tv_cancle = view.findViewById(R.id.tv_cancle);
                TextView tv_geren_fans_cancle = view.findViewById(R.id.tv_geren_fans_cancle);
                TextView img_guanbi = view.findViewById(R.id.img_guanbi);
                tv_geren_fans_cancle.setText("您还未实名认证和(或)绑定银行账户");
                tv_cancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });
                img_guanbi.setText("立即前往");
                img_guanbi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                        Intent intent = new Intent(My_Wallet_Withdraw_Activity.this, My_Wallet_RealName_Activity.class);
                        intent.putExtra("access_token", access_token);
                        startActivity(intent);
                    }
                });
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_my_wallet_withdraw_Url + access_token, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("wallet", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONObject info = jsonObject.getJSONObject("info");
                        String mark = info.getString("mark");//标识
                        limit_money = info.getString("limit_money");//最小提现金额
                        fee_lv = info.getString("fee_lv");//提现费率 %
                        String proportion = info.getString("proportion");//布丁-》RMB 兑换比例
                        tv_wallet_withdraw_limit_money_yb.setText("" + proportion);
                    } else {
                        ToolUtils.showToast(My_Wallet_Withdraw_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(My_Wallet_Withdraw_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
